#####TO DO:#####
## - how run multiple page tests?
## -exception handling (basics are done)
## -screenshot at failure esp for debugging.
## -data driven with proper error tracking
## -integration into hudson preferably with reports as useful as HTMLTestRunner
## -learn SVN so can be administrator of test scripts
## make reports more obvious which browser failed. can append browser name in report.

import inspect, time, re,  os
import base, xmlrunner, unittest
from selenium import selenium
from testconfig import config

class TESTVerifications(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        #self.browserName = config['TEST_CONFIG']['browserName']
        self.selenium = selenium("localhost", 4444, "*firefox3", "https://workflowy.com/")
        ##needed for multiprocessing
#        rc= config['rc_configuration']['command']
#        self.selenium = selenium("localhost", 4444, rc, "https://workflowy.com/")
        self.selenium.start() 
        self.selenium.set_timeout("60000")
        print("setUpClass")      
        self.selenium.window_maximize()
        self.selenium.open("/")
        
    
    def setUp(self):
        self.verificationErrors = []
        
    def test_verification1_error(self):
        try: self.assertEqual("This application is designed", "This application is designedZZZZ",(inspect.stack()[0][3]) +" text missing 'This application is designed'")
        except AssertionError, e: self.verificationErrors.append(str(e))
    def test_verification2_error_three_times(self):
        ##No such element exception
        sel = self.selenium
        try:
            #will confirm element present before interacting with it. Can make a function.
            self.assertTrue(self.selenium.is_element_present("css=form[id=register] > input.submitZZZ"),(inspect.stack()[0][3]) +" Exception Catch: No Element")
            self.selenium.click("css=form[id=register] > input.submitZZZ")
        except AssertionError, e: self.verificationErrors.append(str(e))
        try: self.assertEqual("First failure", "First failureZZZZ",(inspect.stack()[0][3]) +" First failure'")
        except AssertionError, e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Second Failure", "Second FailureZZZZ",(inspect.stack()[0][3]) +" Second failure'")
        except AssertionError, e: self.verificationErrors.append(str(e))
        
    
    def test_verification3_pass(self):
        try: self.assertEqual("This application is designed", "This application is designed",(inspect.stack()[0][3]) +" text missing 'This application is designed'")
        except AssertionError, e: self.verificationErrors.append(str(e))
    
    def test_assertion_4_pass(self):
        self.assertEqual("This application is designed", "This application is designed",(inspect.stack()[0][3]) +" text missing 'This application is designed'")
    
    def test_assertion_5_fail(self):
        self.assertEqual("This application is designed", "This application is designedZZZ",(inspect.stack()[0][3]) +" text missing 'This application is designed'")
    
    def test_login_6(self):
        sel = self.selenium
        sel.open("/") 
        sel.type("css=input[id='id_username']",'test+abc010@workflowy.com'  )
        sel.type("css=input[id='id_password']",'password')
        sel.click("css=form[id='login'] > input.submit")
        sel.wait_for_page_to_load("60000")
        self.failUnless(sel.is_element_present("id=logout"))
    
    def test_home_7(self):
        self.selenium.open("/")    
    
    def tearDown(self):
        #self.selenium.stop()
        self.assertEqual([], self.verificationErrors,"Results: " + str(self.verificationErrors))
    @classmethod    
    def tearDownClass(self):
        
        self.selenium.stop()
        print("tearDownClass")

if __name__ == "__main__":
    unittest.main()
#    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))